var bodyParser = require('body-parser')
var urlencodedParser = bodyParser.urlencoded({
    extended: false
})
var express = require('express')
var db = require('../db.js')
var md5 = require('md5-nodejs')
var multer = require('multer');
var router = express.Router()

router.get('/get-roles', async function (req, res) {
    const { roles } = req.query
    let sql = `select items from roles where role in (?)`
    // let sql = `select items from roles where role in ('editor','admin')`
    const r = await db.doSQL(sql, [roles])

    res.send(db.handleResults(r.data[0].items))
})
// 获取所以角色信息
router.get('/get-all-roles', async function (req, res) {
    let sql = `select * from roles order by id desc `
    const { data } = await db.doSQL(sql)
    res.send(db.handleResults(data))
})

// 删除
router.get('/deleteRoles', async function (req, res) {
    var ids = req.query.id;
    var sql = `delete from roles where id in (${ids})`
    const data = await db.doSQL(sql)
    res.send(db.handleResults(data))
})
// 修改
router.post('/updateRoles', urlencodedParser, async function (req, res) {
    var {
        id,
        role,
        title
    } = req.body
    var sql = `update roles set role='${role}',title='${title}' where id='${id}'`
    const {
        data
    } = await db.doSQL(sql)
    res.send(db.handleResults(data))
})
// 修改组件
router.post('/updateRoutesItem', urlencodedParser, async function (req, res) {
    var {
        id,
        items
    } = req.body
    var sql = `update roles set items='${items}' where id in (${id})`
    const {
        data
    } = await db.doSQL(sql)
    res.send(db.handleResults(data))
})
// 新增
router.post('/addroles', urlencodedParser, async function (req, res) {
    var {
        role,
        title
    } = req.body
    var sql = `insert into roles (role,title) values ("${role}","${title}")`
    const {
        data
    } = await db.doSQL(sql)
    // console.log(data)
    res.send(db.handleResults(data))
})


module.exports = router;